package com.ch999.detect.utils;

import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;

import java.util.List;

import static android.hardware.Camera.getNumberOfCameras;

/**
 * Created by zhulang on 2018/1/16.
 */

public class CameraUtils {
    public static final int CAMERA_FACING_BACK = 0;
    public static final int CAMERA_FACING_FRONT = 1;
    public static final int CAMERA_NONE = 2;

    public static int HasBackCamera() {
        int numberOfCameras = getNumberOfCameras();
        CameraInfo cameraInfo = new CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == CAMERA_FACING_BACK) {
                return i;
            }
        }
        return 2;
    }

    public static int HasFrontCamera() {
        int numberOfCameras = getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == CAMERA_FACING_FRONT) {
                return i;
            }
        }
        return 2;
    }

    public static String getCameraPixels(int paramInt) {
        try {
            if (paramInt == 2)
                return "无";
            Camera localCamera = Camera.open(paramInt);
            Camera.Parameters localParameters = localCamera.getParameters();
            localParameters.set("camera-id", 1);
            List<Camera.Size> localList = localParameters.getSupportedPictureSizes();
            if (localList != null) {
                int heights[] = new int[localList.size()];
                int widths[] = new int[localList.size()];
                for (int i = 0; i < localList.size(); i++) {
                    Camera.Size size = (Camera.Size) localList.get(i);
                    int sizehieght = size.height;
                    int sizewidth = size.width;
                    heights[i] = sizehieght;
                    widths[i] = sizewidth;
                }
                int pixels = getMaxNumber(heights) * getMaxNumber(widths);
                localCamera.release();
                return String.valueOf(pixels / 10000) + " 万";

            } else return "无";
        } catch (Exception e) {
            e.printStackTrace();
            return "无";
        }

    }

    public static int getMaxNumber(int[] paramArray) {
        int temp = paramArray[0];
        for (int i = 0; i < paramArray.length; i++) {
            if (temp < paramArray[i]) {
                temp = paramArray[i];
            }
        }
        return temp;
    }
}
